<?php

require_once dirname(__FILE__).'/helper.php';

class DocmanInstallerScript
{
	public function install($installer)
	{
		// Joomla's archive handler eats a lot of memory
		if((int) ini_get('memory_limit') < 24) {
			@ini_set('memory_limit', '24M');
		}
		
		$lang = JFactory::getLanguage();
		$lang->load('com_installer');

		$tasks = array('logo', 'copyFiles', 'insertInDb', 'cpanel', 'track' );
		$status = & DMStatus::getInstance();
		
		while($status->get() && $task = array_shift($tasks))
		{
			call_user_func(array('DMInstallHelper', $task));
		}
		
		echo '<ul>';
		foreach($status->getMsgs() as $msg) {
			echo '<li>'.$msg.'</li>';
		}
		echo '</ul>'; 
		return $status->get();
	}
	
	public function uninstall($installer)
	{
		$lang = JFactory::getLanguage();
		$lang->load('com_installer');

		DMInstallHelper::removeFiles();
		DMInstallHelper::deleteFromDb();

		// if there's no more data, we remove the tables
		if( DMInstallHelper::cntDbRecords() == 0 ) {
			DMInstallHelper::removeTables();
		}

		// delete the data folder if it's empty
		if ( DMInstallHelper::cntFiles() == 0 ) {
			DMInstallHelper::removeDmdocuments();
		}
	}
}